package com.xu.cloud.codegenerator.service;

import cn.hutool.core.io.IoUtil;
import com.github.pagehelper.Page;
import com.xu.cloud.codegenerator.convert.ColumnEntityConverter;
import com.xu.cloud.codegenerator.convert.TableEntityConverter;
import com.xu.cloud.codegenerator.pojo.domain.ColumnDO;
import com.xu.cloud.codegenerator.pojo.domain.TableDO;
import com.xu.cloud.codegenerator.pojo.dto.ColumnDTO;
import com.xu.cloud.codegenerator.pojo.dto.TableDTO;
import com.xu.cloud.codegenerator.pojo.http.req.QueryMultipleTablesReq;
import com.xu.cloud.codegenerator.pojo.http.resp.QueryMultipleTablesResp;
import com.xu.cloud.codegenerator.pojo.mapper.MysqlGeneratorMapper;
import com.xu.cloud.codegenerator.util.GeneratorUtil;
import com.xu.cloud.common.util.JsonUtil;
import com.xu.cloud.common.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.zip.ZipOutputStream;

/**
 * @author xuguan
 * @since 2024/1/7
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class GeneratorService {

    private final MysqlGeneratorMapper mysqlGeneratorMapper;

    public QueryMultipleTablesResp queryMultipleTables(QueryMultipleTablesReq queryMultipleTablesReq) {
        String tableName = queryMultipleTablesReq.getTableName();
        final int pageNum = queryMultipleTablesReq.getPageNum();
        final int pageSize = queryMultipleTablesReq.getPageSize();

        log.info("开始查询当前数据源下的表结构, 查询条件tableName={}, pageNum={}, pageSize={}", tableName, pageNum, pageSize);

        final Page<TableDO> page = PageUtil.startPage();
        mysqlGeneratorMapper.queryMultipleTables(tableName);
        PageUtil.closePage(page);

        final QueryMultipleTablesResp queryMultipleTablesResp = new QueryMultipleTablesResp();
        queryMultipleTablesResp.setRecords(page.getResult());
        queryMultipleTablesResp.setTotal(page.getTotal());
        queryMultipleTablesResp.setPageNum(page.getPageNum());
        queryMultipleTablesResp.setPageSize(page.getPageSize());

        log.info("查询当前数据源下的表结构结束, records={}", JsonUtil.toJsonString(queryMultipleTablesResp.getRecords()));

        return queryMultipleTablesResp;
    }

    public byte[] generatorCode(String[] tableNames) {
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final ZipOutputStream zos = new ZipOutputStream(bos);

        for (String tableName : tableNames) {
            // 查询表信息
            final TableDO tableDO = mysqlGeneratorMapper.querySingleTable(tableName);
            // 查询列信息
            final List<ColumnDO> columnDOList = mysqlGeneratorMapper.queryColumns(tableName);

            final TableDTO tableDTO = TableEntityConverter.convertTableDO(tableDO);
            final List<ColumnDTO> columnDTOList = ColumnEntityConverter.convertColumnDOList(columnDOList);
            tableDTO.setColumnDTOList(columnDTOList);

            // 生成代码
            GeneratorUtil.generatorCode(tableDTO, zos);
        }

        IoUtil.close(zos);
        IoUtil.close(bos);

        return bos.toByteArray();
    }
}
